using System;
using System.Runtime.Serialization;

namespace KodeIT.Web
{
    /// <summary>
    /// This class represents the exception thrown when a native error is sent by the native ISAPI Filter.
    /// </summary>
    /// <remarks />
    [Serializable]
    public class NativeException : Exception
    {
        private int _nativeError;

        /// <summary>
        /// Creates an instance of the <see cref="NativeException"/> class.
        /// </summary>
        public NativeException()
            : base()
        {
        }

        /// <summary>
        /// Creates an instance of the <see cref="NativeException"/> class.
        /// </summary>
        /// <param name="message">The message to associate with the exception.</param>
        public NativeException(string message)
            : base(message)
        {
        }

        internal NativeException(int nativeError)
            : base(NativeMethods.FormatMessage(nativeError))
        {
            _nativeError = nativeError;
        }

        /// <summary>
        /// Creates an instance of the <see cref="NativeException"/> class.
        /// </summary>
        /// <param name="message">The message to associate with the exception.</param>
        /// <param name="innerException">The inner exception to associate with the exception.</param>
        public NativeException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        /// <summary>
        /// Creates an instance of the <see cref="NativeException"/> class.
        /// </summary>
        /// <param name="info">The serialization information instance.</param>
        /// <param name="context">The streaming context instance.</param>
        protected NativeException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Gets the native error code associated with the exception.
        /// </summary>
        public int NativeError
        {
            get { return _nativeError; }
        }
    }
}
